package com.seveneight.baijiesteward.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.support.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.bean.UserBean;
import com.seveneight.baijiesteward.http.BaseObserver;
import com.seveneight.baijiesteward.http.JsonFormat;
import com.seveneight.baijiesteward.utils.MClickDialog;
import com.seveneight.baijiesteward.utils.MMessageDialog;
import com.seveneight.baijiesteward.utils.PreferencesUtil;
import com.seveneight.baijiesteward.utils.Setting;
import com.seveneight.baijiesteward.utils.StringUtils;
import com.seveneight.baijiesteward.views.FitStateUI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;
import me.yokeyword.fragmentation_swipeback.SwipeBackActivity;

/**
 * Created by user1 on 2018/1/13.
 */

public abstract class BaseActivity extends SwipeBackActivity {


    public Activity activity;
    public Setting setting;
    private MMessageDialog dialog;
    private MClickDialog clickDialog;
    public JsonFormat jsonFormat;
    public UserBean userBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initLayout();
        super.onCreate(savedInstanceState);
        activity = this;
        EventBus.getDefault().register(this);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        if (!StringUtils.isEmpty(PreferencesUtil.get(getContext(),PreferencesUtil.userinfo,"")))
            userBean= JSON.parseObject(PreferencesUtil.get(getContext(),PreferencesUtil.userinfo,""),UserBean.class);
        getApp().addActivity(activity);

        FitStateUI.setImmersionStateMode(activity);

        ButterKnife.bind(activity);

        initUser();

        initView();

        initData();

        setListener();

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)
            getResources();
        super.onConfigurationChanged(newConfig);
    }
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();

            res.updateConfiguration(newConfig, res.getDisplayMetrics());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                createConfigurationContext(newConfig);
            } else {
                res.updateConfiguration(newConfig, res.getDisplayMetrics());
            }
        }
        return res;
    }


    //处理回调
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){
        if (messageEvent.getType()==110){
            userBean= JSON.parseObject(PreferencesUtil.get(getContext(),PreferencesUtil.userinfo,""),UserBean.class);
        }else if (messageEvent.getType()==111){
            userBean=null;
        }
    }

    public MyApplication getApp() {
        return MyApplication.getInstance();
    }

    public Activity getContext(){
        return activity;
    }

    public void initUser() {
        setting = Setting.getInstance(activity);
        jsonFormat = JsonFormat.getInstance(activity);
    }

    protected abstract void initLayout();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void setListener();

    @Override
    public void onBackPressedSupport() {
        // 对于 4个类别的主Fragment内的回退back逻辑,已经在其onBackPressedSupport里各自处理了
        super.onBackPressedSupport();
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        // 设置横向(和安卓4.x动画相同)
        return new DefaultHorizontalAnimator();
    }

    public void afterFinish(){
        Handler handler = new Handler();//消除fragment切换时出现的卡顿效果
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 1000);
    }

    public <T> ObservableTransformer<T,T> setThread(){
        return new ObservableTransformer<T,T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public void showDialog(String title){
        dialog = new MMessageDialog(activity,title);
        dialog.showDialog();
    }

    public void showFinishDialog(String title){
        clickDialog = new MClickDialog(activity,title);
        clickDialog.showDialog();
    }

    public void hideDialog(){
        dialog.hideDialog();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getApp().removeActivity(activity);
        EventBus.getDefault().unregister(this);
    }

}
